#include "task_queue.hh"

TaskQueue::TaskQueue(size_t capacity): _if_exit(false), _capacity(capacity){}
TaskQueue::~TaskQueue(){}
void TaskQueue::push(Task* task){
    unique_lock ul(_mutex);
    while(full()){
        _notfull.wait(ul);
    }
    _queue.push(task);
    _notempty.notify_all();
    ul.unlock();
}

Task* TaskQueue::pop(){
    unique_lock ul(_mutex);
    while(empty()){
        if(_if_exit){
            return nullptr;
        }
        _notempty.wait(ul);
    }
    Task* task = _queue.front();
    _queue.pop();
    _notfull.notify_all();
    ul.unlock();
    return task;
}


bool TaskQueue::empty(){
    return _queue.empty();
}

bool TaskQueue::full(){
    return _queue.size() == _capacity;
}

void TaskQueue::setExit(bool status){
    _if_exit = status;
}